<?php
/**
 * Notes:
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/10/15
 * Time: 22:19
 */

namespace app\index\controller;


use app\model\Chat as ChatModel;
use app\model\ChatList;
use app\model\Friends as FriendMode;
use app\validate\ChatValidate;

class Communication
{
    public static function sendUserChat($param){
        echo ("sss");
        $chat_target = $param['user_id'];//获取当前登录用户
        $data = [
            "chat_content" => $param['chat_content']
        ];
        $friend_id = $param['target'];
        $data["user_id"] = $friend_id;
        $ChatValidate = new ChatValidate();
        //验证信息
        if(!$ChatValidate->check($data)) return res(0,$ChatValidate->getError());
        $FriendMode = new FriendMode();
        //核查是否有该好友
        if(!$FriendMode->check($chat_target,$data["user_id"])) return res(0,'您还未添加该用户为好友');
        $data["chat_target"] = $chat_target;
        $data['chat_status'] = 1;
        $data['chat_mod'] = 1;
        $data['chat_time'] = time();
        $ChatModel = new ChatModel();
        //开启事务
        $ChatModel->startTrans();
        //发送消息
        $chat_res = $ChatModel->addChat($data);
        if(!$chat_res){
            $ChatModel->rollback();
            return res(1,' 发送失败');
        }

        $ChatListMode = new ChatList();
        //开启事务
        $ChatListMode -> startTrans();
        $chat_list = [
            'type' => 1,
            'user_id' => $friend_id,
            'chat_target' => $chat_target,
            'chat_num' => '1',
            'chat_content' => $data['chat_content'],
            'push_time' => time()
        ];
        //核查我放推送消息列表
        $checkPush = $ChatListMode->checkPush($friend_id,$chat_target);
        //核查对方推送消息列表
        $checkTarget = $ChatListMode->checkPush($chat_target,$friend_id);
        //如果我方推送列表不存在，则添加
        if($checkPush == null) {
            $checkMyAdd = $ChatListMode->add($chat_list);
            if(!$checkMyAdd){
                //添加失败，回滚数据
                $ChatModel->rollback();
                $ChatListMode->rollback();
                return res(0,'发送失败');
            }
            //清空，作为标识
            $checkPush == null;
        }
        //如果对方推送列表不存在则添加
        if($checkTarget == null){
            $chat_list['user_id'] = $chat_target;
            $chat_list['chat_target'] = $friend_id;
            $checkTargetAdd = $ChatListMode->add($chat_list);
            if(!$checkTargetAdd){
                //添加失败，回滚数据
                $ChatModel->rollback();
                $ChatListMode->rollback();
                return res(0,'发送失败');
            }
            //清空，作为标识
            $checkTarget = null;
        }
        //更新推送消息列表数据集
        $update_data = [
            'chat_content' => $data['chat_content'],
            'push_time' => time(),
        ];
        //如果标识为空，意味着上面未添加数据，下面就要更新数据
        if($checkPush != null) {
            $chat_data = $checkPush;
            //更新我放消息未读数量
            $update_data['chat_num'] = $chat_data['chat_num'] + 1;
            $update_res = $ChatListMode->edit($chat_data['chat_list_id'], $update_data);
            //更新失败数据回滚
            if (!$update_res) {
                $ChatModel->rollback();
                $ChatListMode->rollback();
                return res(0, '发送失败');

            }
        }
        //如果标识为空，意味着上面未添加数据，下面就要更新数据
        if($checkTarget != null) {
            $target_id = $checkTarget['chat_list_id'];
            //更新对方消息未读数量
            $update_data['chat_num'] = $checkTarget['chat_num'] + 1;
            $target_res = $ChatListMode->edit($target_id, $update_data);
            //更新失败数据回滚
            if (!$target_res) {
                $ChatModel->rollback();
                $ChatListMode->rollback();
                return res(0,'发送失败');
            }

        }
        //以上操作皆成功，提交数据
        $ChatModel->commit();
        $ChatListMode->commit();
        $res_data = [
            'method' => 'chat',
            'res_data' => $chat_res
        ];
        return res(1,'发送成功',$res_data);
    }

}